package client.useCases.login;

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import com.awx.client.login.AwxLoginUser;
import com.awx.client.login.AwxLoginUserResponse;
import com.awx.client.login.AwxLoginWindow;

public class MyLoginWindow extends AwxLoginWindow implements ActionListener {

	private JTextField username;
	private JPasswordField password;
	private JButton loginButton;

	public MyLoginWindow() {

		GridLayout layout = new GridLayout( 3, 2 );
		this.setLayout( layout );

		this.add( new JLabel( "User:" ) );
		username = new JTextField();
		this.add( username );


		this.add( new JLabel( "Pass:" ) );
		password = new JPasswordField();
		this.add( password );

		this.loginButton = new JButton( "Login" );
		this.loginButton.addActionListener( this );
		this.add( this.loginButton );

		this.pack();
	}

	public void actionPerformed(ActionEvent e) {
		AwxLoginUser user = this.getLoginUser();
		user.setUserName( this.username.getText() );
		user.setPassword( this.password.getPassword() );
		user.setRole( "Guest" );

		AwxLoginUserResponse rta = this.login();
		if( !rta.isAnswer() ) {
			JOptionPane.showMessageDialog( this,  rta.getMsg() );
		}
	}

}
